package yongxin.kingdee.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import yongxin.kingdee.service.KingdeeService;
import yongxin.kingdee.utils.HttpUtil;
import yongxin.kingdee.utils.ResultUtil;
import yongxin.kingdee.vo.ResultVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 金蝶物料接口实现类
 * @date 2019/1/4 14:24
 */
@Service
public class KingdeeServiceImpl implements KingdeeService {

    @Override
    public ResultVo login(String url, String content) {

        ResponseEntity<String> responseEntity = HttpUtil.httpPost(url, content);
        //获取登录cookie
        if(responseEntity.getStatusCode()==HttpStatus.OK){
            String login_cookie = "";
            Set<String> keys = responseEntity.getHeaders().keySet();
            for(String key:keys){
                if (key.equalsIgnoreCase("Set-Cookie")) {
                    List<String> cookies = responseEntity.getHeaders().get(key);
                    for(String cookie:cookies){
                        if(cookie.startsWith("kdservice-sessionid")){
                            login_cookie=cookie;
                            break;
                        }
                    }
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("cookie",login_cookie);
            return ResultUtil.success(map);
        }

        Map<String,Object> result = JSON.parseObject(responseEntity.getBody());
        return ResultUtil.error(result.get("Message").toString());
    }

    @Override
    public ResultVo view(String cookie, String content) {
        return null;
    }

    @Override
    public ResultVo save(String url,String cookie, String content) {
        //保存
        Map<String,Object> header = new HashMap<>();
        header.put("Cookie",cookie);
        String result = HttpUtil.httpPost(url,header,content);

        JSONObject jsonObject = JSON.parseObject(result);
        Map<String,Object> map = (Map<String, Object>) jsonObject.get("Result");
        Map<String,Object> responseStatus = (Map<String, Object>) map.get("ResponseStatus");
        Boolean isSuccess = (Boolean) responseStatus.get("IsSuccess");
        if(isSuccess){
            return ResultUtil.success();
        }else{
            List<Map<String,Object>> errors = (List<Map<String, Object>>) responseStatus.get("Errors");
            return ResultUtil.error(JSON.toJSONString(errors));
        }
    }

    @Override
    public ResultVo report(String url,String cookie, String content) {
        //保存
        Map<String,Object> header = new HashMap<>();
        header.put("Cookie",cookie);
        String result = HttpUtil.httpPost(url,header,content);

        JSONObject jsonObject = JSON.parseObject(result);

        if(jsonObject.getString("code").equals("200")){
            return ResultUtil.success(jsonObject);
        }else{
            return ResultUtil.error();
        }
    }

}